﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Reflection;


namespace Treasures
{
    public class ChipFigure : BaseElement
    {
        #region fields and props
        public Color ElementColor { get; set; }

        public bool IsCrystaled { get; set; }

        private static ChipFigureBitmaps FigureBitmaps { get; set; }

        #endregion

        #region public methods
        public override void DrawElementFigure(Graphics g, int x, int y)
        {
            int index = Array.IndexOf(Constants.Colors, this.ElementColor);
            if (this.ElementColor == Color.Transparent)
            {
                this.FigureBitmap = new Bitmap(1, 1);
                return;
            }
            if (index != -1)
            {
                this.FigureBitmap = GetBitMapByFile(this.IsCrystaled, index - 1);
            }
            else
            {
                throw new InvalidOperationException("This color is unavailable");
            }
            if (this.FigureBitmap != null)
            {

//                this.FigureBitmap.MakeTransparent();

               // g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                g.DrawImage(this.FigureBitmap, new Rectangle(x + (int)(this.FigureBitmap.Size.Width * ((1 - Scale) / 2)), y + (int)(this.FigureBitmap.Size.Width * ((1 - Scale) / 2)), (int)(this.FigureBitmap.Size.Width * Scale), (int)(this.FigureBitmap.Size.Height * Scale)), 0, 0, this.FigureBitmap.Size.Width, this.FigureBitmap.Size.Height, GraphicsUnit.Pixel);
            }
            else
            {
                throw new IOException("File not found or cant be read. FigureBitmap is null.");
            }
        }

        public override object Clone()
        {
            return new ChipFigure(this);
        }

        #endregion

        #region Ctors
        static ChipFigure()
        {
            FigureBitmaps = new ChipFigureBitmaps(new string[]
            {
                @"res\Blue\BlueHexagon_00000.png", 
                @"res\Blue\BlueHexagonC_00000.png",
                @"res\Red\RedOctagon_00000.png",
                @"res\Red\RedOctagonC_00000.png",
                @"res\Green\GreenRhombus_00000.png",
                @"res\Green\GreenRhombusC_00000.png",
                @"res\Yellow\YellowCircle_00000.png",
                @"res\Yellow\YellowCircleC_00000.png",
                @"res\Orange\OrangeForm_00000.png",
                @"res\Orange\OrangeFormC_00000.png",
                @"res\White\WhiteRectangle_00000.png",
                @"res\White\WhiteRectangleC_00000.png",
                @"res\Violet\VioletOctagon_00000.png",
                @"res\Violet\VioletOctagonC_00000.png"
            });
        }

        public ChipFigure()
        {
            this.ElementColor = Color.Transparent;
            this.IsCrystaled = false;
            this.Scale = 1;
            this.AnimatingPhase = 0;
        }

        public ChipFigure(bool _crystaled, Color color)
            : base()
        {
            this.ElementColor = color;
            this.IsCrystaled = _crystaled;
            int index = Array.IndexOf(Constants.Colors, this.ElementColor);
            if (index == -1)
            {
                throw new InvalidOperationException("This color is unavailable");
            }
            this.FigureBitmap = GetBitMapByFile(IsCrystaled, index - 1);
            this.Scale = 1;
        }

        public ChipFigure(ChipFigure obj)
            : base()
        {
            this.ElementColor = obj.ElementColor;
            this.IsCrystaled = obj.IsCrystaled;
            this.FigureBitmap = new Bitmap(obj.FigureBitmap);
            this.Scale = 1;
        }
        
        #endregion

        #region private methods

        private Bitmap GetBitMapByFile(bool crystaled, int index)
        {
            string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

            if (IsCrystaled)
            {
                return FigureBitmaps[2 * index + 1];
            }
            return FigureBitmaps[2 * index];
        }

        #endregion
    }
}
